package com.zzptc.syl.traffic;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Violation extends AppCompatActivity implements View.OnClickListener{

    Context context=Violation.this;
    private ViewPager vpager_one;
    private ArrayList<Fragment> aList;
    private MyPagerAdapter mAdapter;
    private VideoView video;
    private Button bta;
    private Button btq;
    private ImageView ivone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_violation);
        vpager_one = (ViewPager) findViewById(R.id.vpager_one);
        bta = findViewById(R.id.bt_a);
        btq = findViewById(R.id.bt_q);
        ivone=findViewById(R.id.iv_1);
        bta.setOnClickListener(this);
        btq.setOnClickListener(this);
        aList = new ArrayList<>();
        aList.add(new BlankFragment());
        aList.add(new BlankFragment2());
        mAdapter = new MyPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,aList);
        vpager_one.setAdapter(mAdapter);
        if (ContextCompat.checkSelfPermission(Violation.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(Violation.this,new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }else {
            initVideoPath();
        }

    }

    private void initVideoPath() {
        File file=new File(Environment.getExternalStorageDirectory(),"画.mp4");
//        video.setVideoPath(file.getPath());
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    initVideoPath();
                }else {
                    Toast.makeText(this, "拒绝", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (video!=null){
            video.suspend();
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_q:
                vpager_one.setCurrentItem(0);
                break;
                case R.id.bt_a:
                vpager_one.setCurrentItem(1);
                break;
            case R.id.iv_1:
                setimgview();
                break;

            default:
                break;
        }
    }
    public void setimgview(){
        LayoutInflater inflater=LayoutInflater.from(Violation.this);
        View twoview=inflater.inflate(R.layout.twobig,null);
        final AlertDialog dialog=new AlertDialog.Builder(Violation.this).create();
        ImageView imageView=(ImageView) twoview.findViewById(R.id.big);
        Glide.with(Violation.this).load(R.drawable.weizhang).into(imageView);
        dialog.setView(twoview);
        dialog.show();
    }

}